#!/usr/bin/env python
#coding=utf-8
import re
import urllib
import time
import Queue
import threading

Que = Queue.Queue()
PMUTEX = threading.Lock()

def getHtml(url):
    try:
        page = urllib.urlopen(url)
        html = page.read()
        reg = r'src=[",\'](.+?\.jpg)[",\']'
        imgre = re.compile(reg)
        imglist = imgre.findall(html)
        for imgurl in imglist:
            print imgurl
            Que.put(imgurl)
        return html
    except Exception,e:
        print url +' open failed'

def getImg():
    imgnum = imgurl.rfind('/')+1
    imgname = imgurl[imgnum:]
    try:
        urllib.urlretrieve(imgurl,'./1/%s' % imgname)
    except Exception,e:
        print imgurl +' open failed'

def write(_filepath, _content):
    try:
        fp = open(_filepath, 'a')
        fp.write(_content)
    except Exception:
        pass
    finally:
        fp.close()

class ThreadMain(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        while Que.qsize() > 0:
            PMUTEX.acquire()
            imgurl = Que.get()
            PMUTEX.release()
            imgnum = imgurl.rfind('/')+1
            imgname = imgurl[imgnum:]
            try:
                urllib.urlretrieve(imgurl,'./1/%s' % imgname)
            except Exception,e:
                print imgurl +' open failed'

if __name__ == '__main__':
    url = raw_input("please input url:")
    print "The url: ", url
    getHtml(url)
    #getImg(html)
    #size = Que.qsize()
    #for x in xrange(1,size):
    #    print Que.get()

    m_threads = 20
    startTime = time.time()
    thread_pool = []
    for i in range(m_threads):
        t = ThreadMain()
        t.setDaemon(True)
        thread_pool.append(t)

    for i in range(m_threads):
        thread_pool[i].start()

    for i in range(m_threads):
        threading.Thread.join(thread_pool[i])
    endTime = time.time()
    spendTime = endTime-startTime
    print "Crack end! Time:", spendTime
